package com.ming.transaction.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.transaction.common.Result;
import com.ming.transaction.entity.Dialy;
import com.ming.transaction.mapper.DialyMapper;
import com.ming.transaction.service.IDialyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 日志管理 服务实现类
 * </p>
 *
 * @author 陈仕健
 * @since 2023-05-08
 */
@Service
public class DialyServiceImpl extends ServiceImpl<DialyMapper, Dialy> implements IDialyService {

    /**
     * 数据导出
     * @param response
     */
    @Override
    public void export(HttpServletResponse response) {
        // 查询要导出的数据
        List<Dialy> dialys = list();
        Workbook work = ExcelExportUtil.exportExcel(new ExportParams("日志表", "日志信息"), Dialy.class, dialys);
        try {
            // 下载到本地
            //FileOutputStream fos = new FileOutputStream( expPath + "日志表.xls");
            //work.write(fos);
            //fos.close();
            // 前端游览器下载
            response.reset();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("日志表","UTF-8") + ".xls");
            work.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 管理员删除日志
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        if (removeById(id)){
            return Result.success("删除日志成功!");
        }
        return Result.fail("删除日志失败！");
    }

    /**
     * 管理员批量删除日志
     * @param ids
     * @return
     */
    @Override
    public Result deleteIds(List<Integer> ids) {
        if (removeByIds(ids)){
            return Result.success("批量删除日志成功!");
        }
        return Result.fail("批量删除日志失败！");
    }

    /**
     * 分页查询页面
     * @param pageNum
     * @param pageSize
     * @param unumber
     * @param conmethod
     * @return
     */
    @Override
    public Result pageSearch(Integer pageNum, Integer pageSize, String unumber, String conmethod) {
        IPage<Dialy> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Dialy> queryWrapper = new QueryWrapper<>();
        if (!"".equals(unumber)) {
            queryWrapper.like("unumber", unumber);
        }
        if (!"".equals(conmethod)) {
            queryWrapper.like("conmethod", conmethod);
        }
        queryWrapper.orderByDesc("create_date");
        return Result.success("日志查询成功!", page(page, queryWrapper));
    }
}
